#include <stdio.h>
#include <memory.h>
int A[100001][11];
int B[100001][12];
int main ()
{
int n,i,j,x,t,max,max_time;
while (scanf("%d",&n)!=EOF)
{
    max_time=0;
  if (n==0)
    break;
  memset(A,0,sizeof(A));
    memset(B,0,sizeof(B));
  for (i=0;i<n;i++)
  {
    scanf("%d%d",&x,&t);
    A[t][x]++;
      if (max_time<t)
          max_time=t;
  }
  for (i=0;i<11;i++)
  {
    B[max_time][i]=A[max_time][i];
  }
  for (i=max_time-1;i>=0;i--)
  {
    if (B[i+1][0]>B[i+1][1])
    {B[i][0]=B[i+1][0]+A[i][0];}
    else
    {B[i][0]=B[i+1][1]+A[i][0];}
    for (j=1;j<10;j++)
    {
      max=B[i+1][j];
      if (B[i+1][j-1]>max)
        max=B[i+1][j-1];
      if (B[i+1][j+1]>max)
        max=B[i+1][j+1];
      B[i][j]=max+A[i][j];
    }
    if (B[i+1][10]>B[i+1][9])
    {B[i][10]=B[i+1][10]+A[i][10];}
    else 
    {B[i][10]=B[i+1][9]+A[i][10];}
  }
  printf("%d\n",B[0][5]);
}
return 0;
}
